package org.dromara.system.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.bo.WbCandidateInfoBo;
import org.dromara.system.domain.bo.WbCandidateListBo;
import org.dromara.system.domain.vo.WbCandidateInfoVo;
import org.dromara.system.domain.vo.WbCandidateListVo;
import org.dromara.system.service.IWbCandidateInfoService;
import org.dromara.system.service.IWbCandidateListService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class WbCandidateInfoListener extends AnalysisEventListener<WbCandidateInfoVo> implements ExcelListener<WbCandidateInfoVo> {

    private final IWbCandidateInfoService wbCandidateInfoService;


    private int successNum = 0;
    private int failureNum = 0;
    private int updateNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public WbCandidateInfoListener() {
        this.wbCandidateInfoService = SpringUtils.getBean(IWbCandidateInfoService.class);
    }

    @Override
    public void invoke(WbCandidateInfoVo wbCandidateInfoVo, AnalysisContext context) {
        try {
            if( StringUtils.isNotEmpty(wbCandidateInfoVo.getTenderee()) ){
                WbCandidateInfoBo wbCandidateInfoBo = BeanUtil.toBean(wbCandidateInfoVo, WbCandidateInfoBo.class);

                WbCandidateInfoBo wbCandidateInfoBo1 = new WbCandidateInfoBo();
                if (StringUtils.isNotEmpty(wbCandidateInfoBo.getHeadline())){
                    wbCandidateInfoBo1.setHeadline(wbCandidateInfoBo.getHeadline());
                }
                //导入更新 如果没数据就新增  有就更新
                List<WbCandidateInfoVo> wbCandidateInfoVos = wbCandidateInfoService.queryList(wbCandidateInfoBo1);
                if (wbCandidateInfoVos.size() <= 0) {
                    wbCandidateInfoService.insertByBo(wbCandidateInfoBo);
                    successNum++;
                }else {
                    Long id = wbCandidateInfoVos.get(0).getId();
                    wbCandidateInfoBo.setId(id);
                    wbCandidateInfoService.updateByBo(wbCandidateInfoBo);
                    updateNum++;
                }
                successMsg.append("<br/>").append(successNum).append("、分标编号 ").append(wbCandidateInfoVo.getTenderee()).append(" 导入成功");
            }else{
                failureNum++;
                String msg = "<br/>" + failureNum + "、分标编号 " + wbCandidateInfoVo.getTenderee() + " 导入失败：";
                failureMsg.append(msg).append("无项目ID或文件缺失分标编号");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、分标编号 " + wbCandidateInfoVo.getTenderee() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<WbCandidateInfoVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！新增共 " + successNum + "，修改共，"+ updateNum +"条， 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<WbCandidateInfoVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
